package com.yc.vote;

import com.yc.vote.vo.UploadProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@ConditionalOnProperty(prefix = "upload" ,name = "enable")
public class UploadConfig implements WebMvcConfigurer {

    @Autowired
    UploadProperties properties;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        properties.getMapping().forEach( (k,v) -> {
            //k => image  v => D:/...
            //file:：表示指定的资源是文件系统中的文件，而不是通过类路径或网络等其他方式获取。
            registry.addResourceHandler("/" + k + "/**").addResourceLocations("file:" + v + "/");
        });

        registry.addResourceHandler("/image/**").addResourceLocations("file:"+"D:/user/mavenImage/");
    }
}